<template>
  <div class="goods">
    <div class="goods" id="goods" name="goods">
      <!-- 面包屑 -->
      <div class="breadcrumb">
        <div class="el-breadcrumb">
          <span class="el-breadcrumb-item">
            <span class="el-breadcrumb-inner is-link">首页</span>
            <i class="el-icon-arrow-right"></i>
          </span>
          <span class="el-breadcrumb-item">
            <span class="el-breadcrumb-inner">全部商品</span>
            <i class="el-icon-arrow-right"></i>
          </span>
          <span class="el-breadcrumb-item">
            <span class="el-breadcrumb-inner">分类</span>
            <i v-show="$route.query.search" class="el-icon-arrow-right"></i>
          </span>
          <span class="el-breadcrumb-item" v-show="$route.query.search">
            <span class="el-breadcrumb-inner">{{ $route.query.search }}</span>
          </span>
        </div>
      </div>
      <!-- 面包屑end -->
      <!-- 分类 -->
      <div class="nav">
        <div class="product-nav">
          <div class="title">分类</div>
          <div class="el-tabs-header">
            <div class="el-tabs-nav-wrap">
              <div class="el-tabs-nav">
                <el-tabs v-model="activeName" type="card">
                  <el-tab-pane
                    v-for="item in categoryList"
                    :key="item.category_id"
                    :label="item.category_name"
                    :name="'' + item.category_id"
                  />
                </el-tabs>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- 分类end -->
      <!-- 商品栏 -->

      <div class="main" v-show="goodsList && goodsList.length">
        <div class="list">
          <div class="myList">
            <ul>
              <li v-for="goods in goodsList" :key="goods.product_id">
                <router-link
                  :to="{
                    path: '/goods/attr',
                    query: {
                      productID: goods.product_id
                    }
                  }"
                >
                  <img :src="$target + goods.product_picture" />
                  <h2>{{ goods.product_name }}</h2>
                  <h3>{{ goods.product_title }}</h3>
                  <p>
                    <span>{{ goods.product_selling_price }}</span>
                    <span class="del">{{ goods.product_price }}</span>
                  </p>
                </router-link>
              </li>
            </ul>
          </div>
        </div>
      </div>
      <div class="goods-titles" v-show="goodsList && !goodsList.length">
        <h4>抱歉，没有找到与“{{ $route.query.search }}”相关的商品</h4>
        <ul>
          <li>建议您：</li>
          <li>1、适当减少筛选条件</li>
          <li>2、调整价格区间</li>
          <li>3、尝试其他关键字</li>
        </ul>
      </div>

      <!-- 商品栏end -->
      <!-- 页码 -->

      <!-- 页码end -->
    </div>
    <div class="block" v-show="goodsList && goodsList.length">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-size="pageSize"
        layout=" prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: 'WGoods',
  data() {
    return {
      goodsList: [],
      page: 1,
      pageSize: 15,
      total: 0,
      categoryID: '',
      id: 0,
      categoryList: '',
      activeName: 0,
      search: ''
    }
  },
  created() {
    this.getCategory()
    this.getgoodsList()
  },
  activated() {
    this.activeName = '-1' // 初始化分类列表当前选中的id为-1
    this.total = 0 // 初始化商品总量为0
    this.currentPage = 1 //初始化当前页码为1
    // 如果路由没有传递参数，默认为显示全部商品
    if (Object.keys(this.$route.query).length == 0) {
      this.categoryID = []
      this.activeName = '0'
      return
    }
    // 如果路由传递了categoryID，则显示对应的分类商品
    if (this.$route.query.categoryID != undefined) {
      this.categoryID = this.$route.query.categoryID
      if (this.categoryID.length == 1) {
        this.activeName = '' + this.categoryID[0]
      }
      return
    }
  },
  watch: {
    activeName(newval) {
      if (newval > 0) {
        this.categoryID = [Number(newval)]
      }
      if (newval == 0) {
        this.categoryID = []
      }
      // 初始化商品总量和当前页码
      this.total = 0
      this.page = 1
      // 更新地址栏链接，方便刷新页面可以回到原来的页面
      this.$router.push({
        path: '/goods',
        query: { categoryID: this.categoryID }
      })
      delete this.$route.query.search
    },
    // 监听分类id，响应相应的商品
    categoryID() {
      this.getgoodsList()
    },
    $route: {
      handler(route) {
        if (!route.query.search) {
          return
        }
        this.getsearchGoods()
      }
    },
    
  },
  methods: {
    getgoodsList() {
      const api =
        this.categoryID.length == 0 && !this.$route.query.search
          ? '/api/product/getAllProduct'
          : '/api/product/getProductByCategory'
      this.$axios
        .post(api, {
          categoryID: this.categoryID,
          currentPage: this.page,
          pageSize: this.pageSize
        })
        .then((res) => {
          this.goodsList = res.data.Product
          this.total = res.data.total
        })
        .catch((err) => {
          return Promise.reject(err)
        })
    },
    getCategory() {
      this.$axios
        .post('/api/product/getCategory', {})
        .then((res) => {
          const val = {
            category_id: 0,
            category_name: '全部'
          }
          const cate = res.data.category
          cate.unshift(val)
          this.categoryList = cate
        })
        .catch((err) => {
          return Promise.reject(err)
        })
    },
    handleCurrentChange(val) {
      this.page = val
      this.getgoodsList()
    },
    getsearchGoods() {
      this.search = this.$route.query.search
      if (!this.search) {
        alert('搜索条件不能为空')
        return
      }
      this.$axios
        .post('/api/product/getProductBySearch', {
          search: this.search,
          currentPage: this.page,
          pageSize: this.pageSize
        })
        .then((res) => {
          this.goodsList = res.data.Product
          this.total = res.data.total
        })
        .catch((err) => {
          return Promise.reject(err)
        })
    }
  }
}
</script>

<style lang="less" scoped>
.goods-titles {
  width: 1225px;
  margin: 20px auto;
  height: 665px;
  ul {
    width: 200px;
  }
}
.goods {
  padding-top: 20px;
  margin: 0 auto;
  background-color: #f5f5f5;
  overflow: hidden;
}

.breadcrumb {
  height: 50px;
  background-color: #fff;
}

.el-breadcrumb {
  width: 1225px;
  line-height: 30px;
  font-size: 16px;
  margin: 0 auto;
}

.el-breadcrumb-item {
  display: block;
  float: left;
}

.el-icon-arrow-right {
  margin: 0 6px;
  font-weight: 400;
  color: #c0c4cc;
  font-style: normal;
}
.el-icon-arrow-right-p {
  display: block;
  font-size: 12px;
  font-weight: 700;
  font-style: normal;
  cursor: not-allowed;
  text-align: center;
  color: #c0c4cc;
}

.el-breadcrumb-inner {
  color: #606266;
}

.nav {
  background-color: #fff;
}

.product-nav {
  width: 1225px;
  height: 40px;
  line-height: 40px;
  margin: 0 auto;
}

.nav .product-nav .title {
  width: 50px;
  font-size: 16px;
  font-weight: 700;
  float: left;
}

.el-tabs-header {
  border-bottom: 1px solid #e4e7ed;
  padding: 0;
  position: relative;
  margin: 0 0 15px;
}

.el-tabs-nav-wrap {
  overflow: hidden;
  margin-bottom: -1px;
  position: relative;
}

.el-tabs-nav {
  border: 1px solid #e4e7ed !important;
  border-bottom: none;
  border-radius: 4px 4px 0 0;
  box-sizing: border-box;
  float: left;
  border-left: none !important;
}

.el-tabs-item {
  float: left;
  padding: 0 20px;
  height: 40px;
  display: inline-block;
  list-style: none;
  font-size: 14px;
  font-weight: 500;
  position: relative;
  border-left: 1px solid #e4e7ed !important;
}

.el-tabs-item:hover {
  color: #409eff;
  cursor: pointer;
}

#tab-0 {
  border: none;
}

.action {
  color: #409eff;
}

.main {
  margin: 0 auto;
  max-width: 1225px;
  padding: 0;
}

.list {
  margin: 0;
  border: 0;
  padding-top: 14.5px;
  margin-left: -13.7px;
  /* overflow:auto; */
}

.myList {
  padding: 0;
  margin: 0;
  border: 0;
  list-style: none;
}

.myList ul {
  margin: 0;
  border: 0;
  padding: 0;
  list-style: none;
}

.myList ul li {
  z-index: 1;
  float: left;
  width: 234px;
  height: 280px;
  padding: 10px 0;
  margin: 0 0 14.5px 13.7px;
  background-color: #fff;
  transition: all 0.2s linear;
  position: relative;
  border: 0;
  transition: all 0.2s linear;
}
.myList ul li:hover {
  box-shadow: 0 15px 30px rgb(0 0 0 / 10%);
  transform: translate3d(0, -2px, 0);
}

.myList ul li a {
  text-decoration: none;
}

.myList ul li a img {
  display: block;
  width: 160px;
  height: 160px;
  margin: 0 auto;
}
.myList ul li a h2 {
  font-weight: 400;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
  margin: 25px 10px 0;
  font-size: 14px;
  color: #333;
}
.myList ul li a h3 {
  margin: 5px 10px;
  height: 18px;
  font-size: 12px;
  color: #b0b0b0;
  font-weight: 400;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
.myList ul li a p {
  margin: 10px;
  text-align: center;
  color: #ff6700;
}
.myList ul li a p .del {
  margin-left: 0.5em;
  color: #b0b0b0;
  text-decoration: line-through;
}

.pagination {
  height: 50px;
  text-align: center;
}
.el-pagination {
  white-space: nowrap;
  padding: 2px 5px;
  color: #303133;
  font-weight: 700;
}
.el-pagination .btn-prev,
.btn-next {
  color: #c0c4cc;
  margin: 0 5px;
  background-color: #f4f4f5;
  min-width: 30px;
  border-radius: 2px;
  border: 0;
}
.el-pagination .btn-prev i,
.btn-next i {
  display: block;
  font-size: 15px;
  font-weight: 700;
}
.el-pagination .btn-next i {
  color: #606266;
}
.el-pagination ul {
  margin: 0;
  display: inline-block;
  list-style: none;
  padding: 0;
}

.el-pagination ul li {
  display: inline-block;
  margin: 0 5px;
  min-width: 30px;
  border-radius: 2px;
  cursor: pointer;
  padding: 0 4px;
  font-size: 13px;
  height: 28px;
  line-height: 28px;
  box-sizing: border-box;
  text-align: center;
  vertical-align: top;
}

.el-pagination ul li:hover {
  color: #409eff;
}
.active {
  background-color: #409eff;
  color: #fff;
  cursor: default;
}

.is-link {
  color: #303133;
  font-weight: 700;
}

.is-link:hover {
  color: #409eff;
  cursor: pointer;
}
.block {
  text-align: center;
  padding: 10px 0;
}
</style>
